package com.apes.hr.transfer.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.transfer.model.WorkHandover;
import com.apes.hr.transfer.repository.WorkHandoverRepository;
import com.apes.scm.masterdata.party.model.Party;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能：工作交接单
 *
 * @author HuJianlin
 * @create 2018-03-19 14:35
 */
@Service("hrWorkHandoverService")
public class WorkHandoverService extends DomainService {

    @Autowired
    public WorkHandoverRepository workHandoverRepository;

    public WorkHandover save(SimpleRequest request) {
        WorkHandover workHandover = request.getO(WorkHandover.class);
        workHandover.setState("create");
        return create(workHandover);
    }

    public WorkHandover create(WorkHandover workHandover) {
        return workHandoverRepository.saveAndFlush(workHandover);
    }

    public String delete(SimpleRequest request) throws Exception {
        String id = request.get("id");
        long versions = (int) request.get("version");

        WorkHandover workHandover = workHandoverRepository.findOne(id);
        if (workHandover == null) throw new RuntimeException("工作交接单【" + id + "】在系统中不存在。");
        if (workHandover.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        if (workHandover.getConfirmUid() != null) throw new RuntimeException("工作交接单【" + id + "】已确认，不能删除。");

        workHandover.setState("delete");
        workHandoverRepository.saveAndFlush(workHandover);

        return "删除成功！";
    }

    /**
     * 功能：审核工作交接单
     *
     * @param request
     * @return
     */
    public WorkHandover confirm(SimpleRequest request) {
        // todo 需要传入一个人员的 Party 对象
        String id = request.get("id");
        long versions = (int) request.get("version");
        Party person = new Party();
        return confirmOrder(id, person, versions);
    }

    /**
     *
     * @param id  工作交接单号
     * @param versions 版本号
     * @return
     */
    public WorkHandover confirmOrder(String id, Party person, long versions) {
        WorkHandover workHandover = workHandoverRepository.findOne(id);
        if (workHandover == null) throw new RuntimeException("工作交接单【" + id + "】在系统中不存在。");
        if (workHandover.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        if (workHandover.getConfirmDate() != null) throw new RuntimeException("工作交接单【" + id + "】在系统中已确认。");

        workHandover.setState("confirm");
        workHandover.setConfirmDate(new Date());
        workHandover = workHandoverRepository.save(workHandover);
        return workHandover;
    }

    /**
     * 功能：查询工作交接单
     * @param request
     * @return
     * @throws Exception
     */
    public Page<WorkHandover> find(SimpleRequest request) throws Exception {
        List<Map> filters = request.get("condition");
        return workHandoverRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size")));
    }

    public WorkHandover findOne(SimpleRequest request){
        WorkHandover workHandover = workHandoverRepository.findOne((String) request.get("id"));
        if(workHandover == null){
            List<WorkHandover> workHandovers = workHandoverRepository.findByIdAndState(request.get("id"), "confirmed");
            if(!workHandovers.isEmpty()) {
                workHandover = workHandovers.stream().findFirst().get();
            }
        }
        return workHandover;
    }
}
